package com.cy.ppj.service.message.impl;

import com.cy.ppj.comm.annotation.Message;
import com.cy.ppj.comm.utils.CommonUtils;
import com.cy.ppj.comm.utils.SpringELUtils;
import com.cy.ppj.dao.mapper.message.MessageTemplateMapper;
import com.cy.ppj.model.domian.message.MessageRecord;
import com.cy.ppj.model.domian.message.MessageTemplate;
import com.cy.ppj.model.dto.message.WithdrawMessageDTO;
import com.cy.ppj.model.dto.wechat.WecahtTemplateDataDTO;
import com.cy.ppj.model.dto.wechat.WechatTemplateDTO;
import com.cy.ppj.model.enums.MessageType;
import com.cy.ppj.model.enums.wechat.WechatTemplate;
import com.cy.ppj.service.wechat.WechatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @description: 订单消息
 * @author: Ocean
 * @create: 2018-08-09 19:15
 **/
@Slf4j
@Message(type = MessageType.WITHDRAW_SUCCESS)
@Service
public class WithdrawMessageImpl extends MessageServiceImpl {

    @Autowired
    private MessageTemplateMapper messageTemplateMapper;
    @Autowired
    private WechatService wechatService;

    @Override
    public void execute(String message) {

        Long withdrawId = Long.valueOf(message);

        WithdrawMessageDTO template =  messageTemplateMapper.selectWithdrawMessageTemplate(withdrawId);
        sendMessage(template);
        sendWechatTemplate(template);
    }

    /**
     * 发送站内信
     *
     * @param template
     */
    private void sendMessage(WithdrawMessageDTO template) {

        MessageTemplate messageTemplate = messageTemplateService.queryMessageTemplateByTypeInCache(MessageType.WITHDRAW_SUCCESS);
        if (messageTemplate == null) {
            log.info("提现成功[{}],没有消息模版。", template.getBankCard());
            return;
        }
        if (!messageTemplate.getOpen()) {
            log.info("提现成功[{}],消息模版未开启。", template.getBankCard());
            return;
        }

        Map<String, Object> map = new HashMap<>();
        map.put("amount", template.getAmount());
        map.put("time", CommonUtils.dateFormat(template.getUpdateTime()));
        map.put("bankName", template.getBankName());
        map.put("bankCard", CommonUtils.bankCardLastFormat(template.getBankCard(), 4));

        String content = SpringELUtils.getValue(messageTemplate.getContent(), map);

        MessageRecord messageRecord = new MessageRecord();
        messageRecord.setType(MessageType.WITHDRAW_SUCCESS);
        messageRecord.setContent(content);
        messageRecord.setUserId(template.getUserId());
        messageRecord.setCreateTime(new Date());
        messageRecord.setUrl("");

        messageRecordService.addMessageRecord(messageRecord);
    }

    /**
     * 发送微信模版
     *
     * @param template
     */
    private void sendWechatTemplate(WithdrawMessageDTO template) {

        if (template.getOpenId() == null) {
            log.info("提现[银行卡:{}]成功,没有绑定微信,无法发送微信消息。", template.getBankCard());
            return;
        }

        WechatTemplateDTO send = new WechatTemplateDTO();
        send.setOpneId(template.getOpenId());
        send.setAccessToken(wechatService.getAccessToken());
        send.setTemplateId(WechatTemplate.WITHDRAW_SUCCESS.getTemplateId());

        WecahtTemplateDataDTO first = new WecahtTemplateDataDTO();
        first.setValue("您的佣金提现申请已审核通过");

        WecahtTemplateDataDTO keyword1 = new WecahtTemplateDataDTO();
        keyword1.setValue(template.getAmount().toString().concat("元"));

        WecahtTemplateDataDTO keyword2 = new WecahtTemplateDataDTO();
        keyword2.setValue(CommonUtils.dateFormat(template.getUpdateTime()));

        String dbBankCard = template.getBankCard();
        String bankCard = template.getBankCard().substring(dbBankCard.length() - 4);
        WecahtTemplateDataDTO keyword3 = new WecahtTemplateDataDTO();
        keyword3.setValue(template.getBankName().concat("(").concat(bankCard).concat(")"));

        WecahtTemplateDataDTO remark = new WecahtTemplateDataDTO();
        remark.setValue("请您留意银行到账时间，如有疑问请咨询客户。");

        send.getData().put("first", first);
        send.getData().put("keyword1", keyword1);
        send.getData().put("keyword2", keyword2);
        send.getData().put("keyword3", keyword3);
        send.getData().put("remark", remark);

        wechatService.sendTemplateMessage(send);
    }
}
